package com.duartepeixinho.P3D.Objects.Primitives  
{
	import com.duartepeixinho.P3D.Core.Geom.Face3;
	import com.duartepeixinho.P3D.Core.Math.Vector3;
	import com.duartepeixinho.P3D.Materials.Materials;
	import com.duartepeixinho.P3D.Objects.Mesh;
	/**
	 * P3D
	 * @version 0.1
	 * @author Duarte Peixinho
	 */
	public class Plane extends Mesh
	{
		private var _w:Number;
		private var _h:Number;		
		
		public function Plane(material:Materials, width:Number = 100, height:Number = 100, segmentsS:uint = 1, segmentsT:uint = 1) 
		{			
			
			_w = width / 2;
			_h = height / 2;			
			var iW:Number = width / segmentsS;
			var iH:Number = height / segmentsT;
			
			// Vertices
			for( var ix:uint = 0; ix < segmentsS +1; ix++ )
			{
				for( var iy:int = 0; iy < segmentsT +1; iy++ )
				{
					var x:Number = ix * iW - width/2;
					var y:Number = iy * iH - height/2;
					geometry.vertices.push( new Vector3( x, y, 0 ) );
				}
			}
			
			// Faces
			for(  ix = 0; ix < segmentsS; ix++ )
			{
				for(  iy= 0; iy < segmentsT; iy++ )
				{
					// Triangle A
					var a:Number = ix     * (segmentsT +1) + iy;
					var c:Number = ix     * (segmentsT +1) + (iy+1);
					var b:Number = (ix+1) * (segmentsT +1) + iy;
					
					geometry.faces.push(new Face3(this, a, b, c, new Vector3(), material));
					//trace(a, b, c);
					// Triangle B
					a = (ix+1) * (segmentsT+1) + (iy+1);
					c = (ix+1) * (segmentsT+1) + iy;
					b = ix     * (segmentsT+1) + (iy + 1);
					//trace(a, b, c);
					geometry.faces.push(new Face3(this, a, b, c, new Vector3(), material));
				}
			}
			
			geometry.calcNormal();
		}
		
	}

}